package main

import (
	"fmt"
	"strings"
	"syscall"

	"gitee.com/wmdng/term"
)

func main() {

	var x, y int
	var sb strings.Builder

	/**/
	if true != term.IsTerminal(syscall.Stdin) {
		fmt.Println("ssssss")
		return
	}

	//
	st, err := term.MakeRaw(syscall.Stdin)
	if err != nil {
		return
	}

	defer term.Restore(syscall.Stdin, st)

	w, h, err := term.GetSize(syscall.Stdin)
	if err != nil {
		fmt.Printf("get size, err, %v, \r", err)
		return
	}

	x = 0
	y = 0
	t := term.NewTerm(w, h)
	sb.Reset()

	for {
		xx, err := t.GetKeys()
		if err != nil {
			fmt.Printf("getkey, err = %v \r\n", err)
			break
		}

		/* CTRL + C */
		if xx == 0x03 {
			break
		}

		/* CTRL + D */
		if xx == 0x04 {

			x = x + 1
			y = y + 1
			t.SetCusorPos(x, y)
			fmt.Printf("x=%v, y=%v, \r\n", x, y)
			continue
		}

		/* ENTER */
		if xx == 0x0D {
			if sb.Len() > 0 {

				/**/
				t.ScrollLines(1)

				/**/
				t.WriteString(sb.String())
				sb.Reset()

				/**/
				t.GotoCliPos()
			}

			continue
		}

		/* other */
		sb.WriteRune(xx)
		fmt.Printf("%c", xx)
		//fmt.Printf("rune = %U, %c\r\n", xx, xx)

	}

	return
}
